package org.mcjug;

import org.mcjug.model.YambaUtils;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class BaseActivity extends Activity {
	private static String TAG = BaseActivity.class.getSimpleName();

	public static String SHOUL_I_NOTIFY_INTENT_ACTION_NAME = "org.mcjug.yamba.shouldInotify";
	public static String DO_NOT_NOTIFY_INTENT_ACTION_NAME = "org.mcjug.yamba.doNotNotify";

	private YambaApplicationInterface yamba;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		yamba = (YambaApplicationInterface) getApplication();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater menuInflater = getMenuInflater();
		menuInflater.inflate(R.menu.menu, menu);
		return true;
	}

	@Override
	protected void onPause() {
		super.onPause();
		unregisterReceiver(needToLoginToTwitter);
		unregisterReceiver(shouldINotifyReciever);
	}

	@Override
	protected void onResume() {
		super.onResume();
		registerReceiver(
				needToLoginToTwitter,
				new IntentFilter(
						getString(R.string.login_to_twitter_intent_action_name)),
				getString(R.string.send_timeline_notifications_permission_name),
				null);

		Log.d(TAG, "about to register should i notify reciever");
		registerReceiver(
				shouldINotifyReciever,
				new IntentFilter(SHOUL_I_NOTIFY_INTENT_ACTION_NAME),
				getString(R.string.send_timeline_notifications_permission_name),
				null);
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		boolean retval = super.onMenuItemSelected(featureId, item);

		if (item.getItemId() == R.id.exititem) {
			finish();
		} else if (item.getItemId() == R.id.startstopserviceitem) {
			YambaUtils.rescheduleAlarm(getApplicationContext());
		} else if (item.getItemId() == R.id.purgestatuses) {
			yamba.getStatusData().delete(-1, null, null);
		} else if (item.getItemId() == R.id.openstatus) {
			Intent intent1 = new Intent(getApplicationContext(),
					StatusActivity.class);
			startActivity(intent1.addFlags(
					Intent.FLAG_ACTIVITY_REORDER_TO_FRONT).addFlags(
					Intent.FLAG_ACTIVITY_SINGLE_TOP));
		} else if (item.getItemId() == R.id.opentimeline) {
			startActivity(new Intent(this, TimelineActivity.class).addFlags(
					Intent.FLAG_ACTIVITY_REORDER_TO_FRONT).addFlags(
					Intent.FLAG_ACTIVITY_SINGLE_TOP));
		} else if (item.getItemId() == R.id.logintotwitter) {
			yamba.loginToTwitter();
		} else if (item.getItemId() == R.id.openpreferences) {
			startActivity(new Intent(this, PrefsActivity.class).addFlags(
					Intent.FLAG_ACTIVITY_REORDER_TO_FRONT).addFlags(
					Intent.FLAG_ACTIVITY_SINGLE_TOP));
		}

		return retval;
	}

	@Override
	public boolean onMenuOpened(int featureId, Menu menu) {
		boolean retval = super.onMenuOpened(featureId, menu);

		return retval;
	}

	BroadcastReceiver needToLoginToTwitter = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			if (intent.getAction().equals(
					getString(R.string.login_to_twitter_intent_action_name))) {
				Toast.makeText(BaseActivity.this.getApplicationContext(),
						getString(R.string.toast_message_twitter_login_needed),
						Toast.LENGTH_LONG).show();
			}
		}
	};

	BroadcastReceiver shouldINotifyReciever = new BroadcastReceiver() {

		@Override
		public void onReceive(Context context, Intent intent) {
			Log.d(TAG,
					"shouldINotifyReciever recieved intent: "
							+ intent.getAction());
			if (intent.getAction().equals(SHOUL_I_NOTIFY_INTENT_ACTION_NAME)) {
				Log.d(TAG,
						"shouldINotifyReciever about to send do not notify broadcast");
				Intent outgoingIntent = new Intent(
						DO_NOT_NOTIFY_INTENT_ACTION_NAME);
				sendBroadcast(
						outgoingIntent,
						getString(R.string.receive_timeline_notifications_permission_name));
			}
		}
	};

}
